// packageB/member/enterprise/enterprise_store/enterprise_store.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    list: [],
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    optype: 1
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.type) {
      this.data.optype = options.type;
    }
    this.getData(); //获取数据
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },
  //获取数据
  getData() {
    let json = {
      page: 1
    };
    let urlStr = '';
    if (this.data.optype == 1) {
      urlStr = app.getNetAddresss('plugin.merchant.frontend.store.list.storeListByStaff');
    } else if (this.data.optype == 2) {
      urlStr = app.getNetAddresss('plugin.merchant.frontend.store.list.storeList');
    }
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            list: resdata.data.store_list.data,
            total_page: resdata.data.store_list.last_page
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  toShop(e) {
    let id = e.currentTarget.dataset.storeid;
    wx.navigateTo({
      url: '/packageC/o2o/HomeSeller/HomeSeller' + '?store_id=' + id
    });
  },
  //获取更多数据
  getMoreData() {
    let json = {
      page: this.data.page
    };
    let urlStr = '';
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      json.page = this.data.page;
      if (this.data.optype == 1) {
        urlStr = app.getNetAddresss('plugin.merchant.frontend.store.list.storeListByStaff');
      } else if (this.data.optype == 2) {
        urlStr = app.getNetAddresss('plugin.merchant.frontend.store.list.storeList');
      }
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (res) => {
          let resdata = res.data;
          if (resdata.result === 1) {
            var myData = resdata.data.store_list.data;
            this.setData({
              list: this.data.list.concat(myData)
            });
            if(this.data.page < resdata.data.store_list.last_page){
              this.data.isLoadMore = true;
            }
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  }
});
